#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>


#include <arpa/inet.h>
#include <netdb.h>

void error_handling(char *msg);


// struct hostent
// {
//   char *h_name;			/* Official name of host.  */
//   char **h_aliases;		/* Alias list.  */
//   int h_addrtype;		/* Host address type.  */
//   int h_length;			/* Length of address.  */
//   char **h_addr_list;		/* List of addresses from name server.  */
// #ifdef __USE_MISC
// # define	h_addr	h_addr_list[0] /* Address, for backward compatibility.*/
// #endif
// };


int main(int argc, char *argv[])
{
    int i;
    struct hostent *host;
    if ( argc != 2) {
        printf("Usage: %s <addr>\n", argv[0]);
        exit(1);
    }

    host = gethostbyname(argv[1]);
    if (!host) {
        error_handling("gethost... error");
    }

    printf("official name: %s \n", host->h_name);
    for (i = 0;host->h_aliases[i];i++) {
        printf("Aliases %d: %s \n", i + 1, host->h_aliases[i]);
    }
    printf("Address type: %s \n",
        (host->h_addrtype == AF_INET) ? "AF_INET":"AF_INET6");
    for (i = 0; host->h_addr_list[i];i++) {
        printf("IP addr %d: %s \n", i + 1, 
            inet_ntoa(*(struct in_addr *)host->h_addr_list[i]));
    }

    return 0;
}

void error_handling(char *msg)
{
    fputs(msg, stderr);
    fputc('\n', stderr);
    exit(1);
}